﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using G.Navigate;

namespace FY13
{
    public partial class MainPage : GBasePage
    {
        // 构造函数
        public MainPage()
        {
            InitializeComponent();
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            var list = this.NavigationService.BackStack.ToArray();
            foreach (var item in list)
            {
                this.NavigationService.RemoveBackEntry();
            }
        }

        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            DoLogin();

        }

        private void DoLogin()
        {
            if (string.IsNullOrEmpty(txtUser.Text))
            {
                MessageBox.Show("请输入您的账号！", "提示", MessageBoxButton.OK);
                return;
            }
            if (string.IsNullOrEmpty(txtPWD.Password))
            {
                MessageBox.Show("请输入您的密码！", "提示", MessageBoxButton.OK);
                return;
            }
            var users = GloabalData.Instance.FYDataBase.Users;
            var user = users.FirstOrDefault(c => c.Email == txtUser.Text);
            if (user == null)
            {
                MessageBox.Show("请输入的账号不存在，请重新输入！", "提示", MessageBoxButton.OK);
                return;
            }
            if (string.IsNullOrEmpty(user.Password))
            {
                if (txtPWD.Password != "MODEA2012")//默认密码为：MODEA2012
                {
                    MessageBox.Show("请输入的密码不正确，请重新输入！", "提示", MessageBoxButton.OK);
                    return;
                }
                this.NavigationService.Navigate(string.Format("/View/ChangePasswordPage.xaml?userid={0}", user.Email));
                //导航到重置密码界面
                return;
            }
            if (user.Password != txtPWD.Password)
            {
                MessageBox.Show("请输入的密码不正确，请重新输入！", "提示", MessageBoxButton.OK);
                return;
            }
            GloabalData.Instance.AutoLoginId = user.Email;
            //导航到主页
            this.NavigationService.Navigate(string.Format("/View/Index.xaml?userid={0}", user.Email));
        }

        private void TextBlock_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                DoLogin();
            }
        }
    }
}